home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / sbin / laptop_mode < prev    next >
Text File  |  2009-10-06  |  32KB  |  936 lines

  1. #! /bin/sh
  2. #
  3. # Script to start or stop laptop_mode, and to control various settings of the
  4. # kernel, hardware etc. that influence power consumption.
  5. #
  6. # This script is a part of Laptop Mode Tools. If you are running a supported
  7. # power management daemon, this script will be automatically called on power
  8. # state change.
  9. #
  10. # Configure laptop mode tools in /etc/laptop-mode/laptop-mode.conf, and in
  11. # the broken-out config files in /etc/laptop-mode/conf.d.
  12. #
  13. # Please consult the manual pages laptop-mode.conf(8) and laptop_mode(8) for
  14. # additional information.
  15. #
  16. # Maintainer:        Bart Samwel (bart@samwel.tk)
  17. # Project home page: http://samwel.tk/laptop_mode
  18. #
  19. # Contributors to this script:   Bart Samwel
  20. #                 Kiko Piris
  21. #                 Micha Feigin
  22. #                 Andrew Morton
  23. #                 Herve Eychenne
  24. #                 Dax Kelson
  25. #                 Jan Polacek
  26. #                 ... and many others that I've stopped
  27. #                 keeping track of.
  28. #
  29. # Based on a script for Linux 2.4 written by Jens Axboe.
  30. #
  31. #############################################################################
  32.  
  33. set -a
  34.  
  35. # The laptop mode tools version number. Extracted by the installer makefile
  36. # as well, so don't change the format!
  37. LMTVERSION=1.47
  38.  
  39. # This script is loaded from multiple scripts to set the config defaults
  40. # and to read the configuration on top of those. Only when the command is
  41. # recognized does this script do anything else.
  42. VERBOSE_OUTPUT=0
  43. ENABLE_LAPTOP_MODE_ON_BATTERY=1
  44. ENABLE_LAPTOP_MODE_ON_AC=0
  45. ENABLE_LAPTOP_MODE_WHEN_LID_CLOSED=0
  46. PARTITIONS="auto /dev/mapper/*"
  47. LM_BATT_MAX_LOST_WORK_SECONDS=600
  48. LM_AC_MAX_LOST_WORK_SECONDS=360
  49. DEF_MAX_AGE=30
  50. LM_READAHEAD=3072
  51. NOLM_READAHEAD=128
  52. CONTROL_READAHEAD=1    
  53. CONTROL_NOATIME=0
  54. USE_RELATIME=1
  55. CONTROL_HD_IDLE_TIMEOUT=1
  56. LM_AC_HD_IDLE_TIMEOUT=4    # 20 seconds
  57. LM_BATT_HD_IDLE_TIMEOUT=4  # 20 seconds
  58. NOLM_HD_IDLE_TIMEOUT=244   # 2 hours
  59. DEF_UPDATE=5
  60. DEF_XFS_AGE_BUFFER=15
  61. DEF_XFS_SYNC_INTERVAL=30
  62. DEF_XFS_BUFD_INTERVAL=1
  63. XFS_HZ=100
  64. CONTROL_MOUNT_OPTIONS=1
  65. BATT_HD_POWERMGMT=1
  66. LM_AC_HD_POWERMGMT=254
  67. NOLM_AC_HD_POWERMGMT=254
  68. CONTROL_HD_POWERMGMT=0
  69. CONTROL_HD_WRITECACHE=0
  70. NOLM_AC_HD_WRITECACHE=1
  71. NOLM_BATT_HD_WRITECACHE=0
  72. LM_HD_WRITECACHE=0
  73. LM_DIRTY_RATIO=60
  74. LM_DIRTY_BACKGROUND_RATIO=1
  75. NOLM_DIRTY_BACKGROUND_RATIO=10
  76. NOLM_DIRTY_RATIO=40
  77. LM_SECONDS_BEFORE_SYNC=2    
  78. BATT_CPU_MAXFREQ=medium
  79. BATT_CPU_MINFREQ=slowest
  80. BATT_CPU_GOVERNOR=ondemand
  81. BATT_CPU_IGNORE_NICE_LOAD=1
  82. LM_AC_CPU_MAXFREQ=fastest
  83. LM_AC_CPU_MINFREQ=slowest
  84. LM_AC_CPU_GOVERNOR=ondemand
  85. LM_AC_CPU_IGNORE_NICE_LOAD=1
  86. NOLM_AC_CPU_MAXFREQ=fastest
  87. NOLM_AC_CPU_MINFREQ=slowest
  88. NOLM_AC_CPU_GOVERNOR=ondemand
  89. NOLM_AC_CPU_IGNORE_NICE_LOAD=0
  90. CONTROL_CPU_FREQUENCY=0
  91. HD="/dev/[hs]d[abcdefgh]"
  92. CONTROL_SYSLOG_CONF=0
  93. LM_AC_SYSLOG_CONF=/etc/syslog-on-ac-with-lm.conf
  94. NOLM_AC_SYSLOG_CONF=/etc/syslog-on-ac-without-lm.conf
  95. BATT_SYSLOG_CONF=/etc/syslog-on-battery.conf
  96. SYSLOG_CONF_SIGNAL_PROGRAM=syslogd
  97. SYSLOG_CONF=/etc/syslog.conf
  98. CONTROL_DPMS_STANDBY=0
  99. BATT_DPMS_STANDBY=300
  100. LM_AC_DPMS_STANDBY=1200
  101. NOLM_AC_DPMS_STANDBY=1200
  102. CONTROL_CPU_THROTTLING=0
  103. BATT_CPU_THROTTLING=medium
  104. LM_AC_CPU_THROTTLING=minimum
  105. NOLM_AC_CPU_THROTTLING=minimum
  106. CONTROL_START_STOP=1
  107. CONTROL_TERMINAL=0
  108. TERMINALS="/dev/vc/1 /dev/vcs1"
  109. BATT_TERMINAL_BLANK_MINUTES=1
  110. BATT_TERMINAL_POWERDOWN_MINUTES=2
  111. LM_AC_TERMINAL_BLANK_MINUTES=10
  112. LM_AC_TERMINAL_POWERDOWN_MINUTES=10
  113. NOLM_AC_TERMINAL_BLANK_MINUTES=10
  114. NOLM_AC_TERMINAL_POWERDOWN_MINUTES=50
  115. ENABLE_AUTO_HIBERNATION=0
  116. HIBERNATE_COMMAND=/usr/sbin/hibernate
  117. AUTO_HIBERNATION_ON_CRITICAL_BATTERY_LEVEL=1
  118. DISABLE_LAPTOP_MODE_ON_CRITICAL_BATTERY_LEVEL=1
  119. AUTO_HIBERNATION_BATTERY_CHARGE_MAH=0
  120. AUTO_HIBERNATION_BATTERY_CHARGE_MWH=0
  121. MINIMUM_BATTERY_CHARGE_MAH=0
  122. MINIMUM_BATTERY_CHARGE_MWH=0
  123. ASSUME_SCSI_IS_SATA=1
  124. CONTROL_BRIGHTNESS=0
  125. BATT_BRIGHTNESS_COMMAND=false
  126. LM_AC_BRIGHTNESS_COMMAND=false
  127. NOLM_AC_BRIGHTNESS_COMMAND=false
  128.  
  129. # No default on these ones -- we need to detect if they have been set, for
  130. # backward compatibility with MINIMUM_BATTERY_MINUTES etc.
  131. AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT=
  132. MINIMUM_BATTERY_CHARGE_PERCENT=
  133.  
  134. # Backward compatibility variable that is sometimes
  135. # set externally (Debian init system)
  136. unset VERBOSE
  137.  
  138. # Source config. Some config settings have been moved from the main config file
  139. # to modular configuration files, and to support existing laptop-mode.conf
  140. # files from earlier versions, we source the modular configuration files FIRST.
  141. if [ -d /etc/laptop-mode/conf.d ] ; then
  142.     for CONF in /etc/laptop-mode/conf.d/*.conf ; do
  143.         if [ -r "$CONF" ] ; then
  144.             . "$CONF"
  145.         else
  146.             echo "Warning: Configuration file $CONF is not readable, skipping."
  147.         fi
  148.     done
  149. fi
  150. if [ -r /etc/laptop-mode/laptop-mode.conf ] ; then
  151.     . /etc/laptop-mode/laptop-mode.conf
  152. else
  153.     echo "$0: Configuration file /etc/laptop-mode/laptop-mode.conf not present or not readable."
  154.     exit 1
  155. fi
  156.  
  157. # Support for old config settings
  158. if [ "$AC_HD" != "" ] ; then
  159.     AC_HD_WITHOUT_LM="$AC_HD"
  160.     AC_HD_WITH_LM="$AC_HD"
  161. fi
  162. if [ "$VERBOSE" != "" ] ; then
  163.     VERBOSE_OUTPUT="$VERBOSE"
  164. fi
  165. if [ "$CPU_MAXFREQ" != "" ] ; then
  166.     BATT_CPU_MAXFREQ="$CPU_MAXFREQ"
  167. fi
  168. if [ "$MAX_AGE" != "" ] ; then
  169.     LM_BATT_MAX_LOST_WORK_SECONDS="$MAX_AGE"
  170.     LM_AC_MAX_LOST_WORK_SECONDS="$MAX_AGE"
  171. fi
  172. if [ "$DEF_AGE" != "" ] ; then
  173.     DEF_MAX_AGE="$DEF_AGE"
  174. fi
  175. if [ "$LAPTOP_MODE_ALWAYS_ON" != "" ] ; then
  176.     ENABLE_LAPTOP_MODE_ALWAYS="$LAPTOP_MODE_ALWAYS_ON"
  177. fi
  178. if [ "$LM_WHEN_LID_CLOSED" != "" ] ; then
  179.     ENABLE_LAPTOP_MODE_WHEN_LID_CLOSED="$LM_WHEN_LID_CLOSED"
  180. fi
  181. if [ "$REMOUNT_PARTITIONS" != "" ] ; then
  182.     PARTITIONS="$REMOUNT_PARTITIONS"
  183. fi
  184. if [ "$READAHEAD" != "" ] ; then
  185.     LM_READAHEAD="$READAHEAD"
  186. fi
  187. if [ "$DO_REMOUNT_NOATIME" != "" ] ; then
  188.     CONTROL_NOATIME="$DO_REMOUNT_NOATIME"
  189. fi
  190. if [ "$DO_HD" != "" ] ; then
  191.     CONTROL_HD_IDLE_TIMEOUT="$DO_HD"
  192. fi
  193. if [ "$AC_HD_WITH_LM" != "" ] ; then
  194.     LM_AC_HD_IDLE_TIMEOUT="$AC_HD_WITH_LM"
  195. fi
  196. if [ "$AC_HD_WITHOUT_LM" != "" ] ; then
  197.     NOLM_HD_IDLE_TIMEOUT="$AC_HD_WITHOUT_LM"
  198. fi
  199. if [ "$BATT_HD" != "" ] ; then
  200.     LM_BATT_HD_IDLE_TIMEOUT="$BATT_HD"
  201. fi        
  202. if [ "$DO_REMOUNTS" != "" ] ; then
  203.     CONTROL_MOUNT_OPTIONS="$DO_REMOUNTS"
  204. fi    
  205. if [ "$DO_HD_POWERMGMT" != "" ] ; then
  206.     CONTROL_HD_POWERMGMT="$DO_HD_POWERMGMT"
  207. fi
  208. if [ "$AC_HDPARM_POWERMGMT_WITH_LM" != "" ] ; then
  209.     LM_AC_HD_POWERMGMT="$AC_HDPARM_POWERMGMT_WITH_LM"
  210. fi
  211. if [ "$AC_HDPARM_POWERMGMT_WITHOUT_LM" != "" ] ; then
  212.     NOLM_AC_HD_POWERMGMT="$AC_HDPARM_POWERMGMT_WITHOUT_LM"
  213. fi
  214. if [ "$BATT_HDPARM_POWERMGMT" != "" ] ; then
  215.     BATT_HD_POWERMGMT="$BATT_HDPARM_POWERMGMT"
  216. fi
  217. if [ "$DO_WRITECACHE" != "" ] ; then
  218.     CONTROL_HD_WRITECACHE="$DO_WRITECACHE"
  219. fi
  220. if [ "$AC_WRITECACHE_WITHOUT_LM" != "" ] ; then
  221.     NOLM_AC_HD_WRITECACHE="$AC_WRITECACHE_WITHOUT_LM"
  222. fi
  223. if [ "$BATT_WRITECACHE" != "" ] ; then
  224.     LM_HD_WRITECACHE="$BATT_WRITECACHE"
  225. fi
  226. if [ "$DIRTY_RATIO" != "" ]; then
  227.     LM_DIRTY_RATIO="$DIRTY_RATIO"
  228. fi
  229. if [ "$DIRTY_BACKGROUND_RATIO" != "" ] ; then
  230.     LM_DIRTY_BACKGROUND_RATIO="$DIRTY_BACKGROUND_RATIO"
  231. fi
  232. if [ "$DEF_DIRTY_RATIO" != "" ]; then
  233.     NOLM_DIRTY_RATIO="$DEF_DIRTY_RATIO"
  234. fi
  235. if [ "$DEF_DIRTY_BACKGROUND_RATIO" != "" ] ; then
  236.     NOLM_DIRTY_BACKGROUND_RATIO="$DEF_DIRTY_BACKGROUND_RATIO"
  237. fi
  238. if [ "$DO_CPU" != "" ] ; then
  239.     CONTROL_CPU_FREQUENCY="$DO_CPU"
  240. fi
  241. if [ "$CONTROL_CPU_MAXFREQ" != "" ] ; then
  242.     CONTROL_CPU_FREQUENCY="$CONTROL_CPU_MAXFREQ"
  243. fi
  244. if [ "$AC_CPU_MAXFREQ_WITH_LM" != "" ] ; then
  245.     LM_AC_CPU_MAXFREQ="$AC_CPU_MAXFREQ_WITH_LM"
  246. fi
  247. if [ "$AC_CPU_MAXFREQ_WITHOUT_LM" != "" ] ; then
  248.     NOLM_AC_CPU_MAXFREQ="$AC_CPU_MAXFREQ_WITHOUT_LM"
  249. fi
  250. if [ "$DO_SYSLOG" != "" ] ; then
  251.     CONTROL_SYSLOG_CONF="$DO_SYSLOG"
  252. fi
  253. if [ "$SYSLOG_SIGNAL_PROGRAM" != "" ] ;then
  254.     SYSLOG_CONF_SIGNAL_PROGRAM="$SYSLOG_SIGNAL_PROGRAM"
  255. fi
  256. if [ "$AC_SYSLOG_WITH_LM" != "" ] ; then
  257.     LM_AC_SYSLOG_CONF="$AC_SYSLOG_WITH_LM"
  258. fi
  259. if [ "$AC_SYSLOG_WITHOUT_LM" != "" ] ; then
  260.     NOLM_AC_SYSLOG_CONF="$AC_SYSLOG_WITHOUT_LM"
  261. fi
  262. if [ "$BATT_SYSLOG" != "" ] ; then
  263.     BATT_SYSLOG_CONF="$BATT_SYSLOG"
  264. fi
  265. if [ "$ENABLE_LAPTOP_MODE_ALWAYS" != "" ] ; then
  266.     ENABLE_LAPTOP_MODE_ON_AC="$ENABLE_LAPTOP_MODE_ALWAYS"
  267. fi
  268. if [ "$MINIMUM_BATTERY_MINUTES" != "" -a "$MINIMUM_BATTERY_CHARGE_PERCENT" = "" ] ; then
  269.     # Use a very conservative estimate (1% = 1 battery minute, 100 minutes in a battery)
  270.     # for backward compatibility.
  271.     MINIMUM_BATTERY_CHARGE_PERCENT="$MINIMUM_BATTERY_MINUTES"
  272. fi
  273. if [ -z "$MINIMUM_BATTERY_CHARGE_PERCENT" ] ; then
  274.     # Apply the default, now that we've determined that this is the minimum.
  275.     MINIMUM_BATTERY_CHARGE_PERCENT=3
  276. fi
  277. if [ "$AUTO_HIBERNATION_BATTERY_MINUTES" != "" -a "$AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT" = "" ] ; then
  278.     # Use a very conservative estimate (1% = 1 battery minute, 100 minutes in a battery)
  279.     # for backward compatibility.
  280.     AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT="$AUTO_HIBERNATION_BATTERY_MINUTES"
  281. fi
  282. if [ -z "$AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT" ] ; then
  283.     # Apply the default, now that we've determined that this is the minimum.
  284.     AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT=2
  285. fi
  286.  
  287. # Postprocessing
  288. if [ "$VERBOSE_OUTPUT" -ne 0 ] ; then
  289.     OUTPUT="/dev/stdout"
  290.     LM_VERBOSE="[ 1 = 1 ]"
  291. else
  292.     OUTPUT="/dev/null"
  293.     LM_VERBOSE="[ 1 = 0 ]"
  294. fi
  295.  
  296. if [ "$PARTITIONS" = "" ] ; then
  297.     PARTITIONS="auto /dev/mapper/*"
  298. fi
  299.  
  300.  
  301. # Expand shell wild cards immediately.
  302. PARTITIONS=$( echo $PARTITIONS )
  303. TERMINALS=$( echo $TERMINALS )
  304.  
  305. # Convert seconds to hdparm -S format
  306. # Everything over 20 minutes is interpreted as 2 hours.
  307. seconds_to_hdparm_S() {
  308.   if [ "$1" -eq 0 ] ; then
  309.     # disable.
  310.     echo 0
  311.  elif [ "$1" -gt 0 -a "$1" -lt 5 ] ; then
  312.     # 5 seconds minimum
  313.     echo 1     
  314.   elif [ "$1" -le $((240*5)) ] ; then
  315.     # Values between 1 and 240 signify increments of 5 seconds
  316.     echo $(($1 / 5))
  317.   elif [ "$1" -lt $((30*60)) ] ; then
  318.     # Values between 20 and 30 minutes are rounded up to 30 minutes.
  319.     echo 241
  320.   elif [ "$1" -lt $((12*30*60)) ] ; then
  321.     # Values between 30 minutes and 6 hours (exclusive) yield values between
  322.     # 241 and 251, in 30-minute increments.
  323.     echo $(( 240 + ($1 / (30*60)) ))
  324.   else
  325.     # Larger values effectively indicate no timeout at all.
  326.     echo 0
  327.   fi
  328. }
  329.  
  330. # Convert configured idle timeouts to hdparm -S format.
  331. if [ "$LM_AC_HD_IDLE_TIMEOUT_SECONDS" != "" ] ; then
  332.     LM_AC_HD_IDLE_TIMEOUT=$(seconds_to_hdparm_S $LM_AC_HD_IDLE_TIMEOUT_SECONDS)
  333. fi
  334. if [ "$LM_BATT_HD_IDLE_TIMEOUT_SECONDS" != "" ] ; then
  335.     LM_BATT_HD_IDLE_TIMEOUT=$(seconds_to_hdparm_S $LM_BATT_HD_IDLE_TIMEOUT_SECONDS)
  336. fi
  337. if [ "$NOLM_HD_IDLE_TIMEOUT_SECONDS" != "" ] ; then
  338.     NOLM_HD_IDLE_TIMEOUT=$(seconds_to_hdparm_S $NOLM_HD_IDLE_TIMEOUT_SECONDS)
  339. fi
  340.  
  341. if [ "$1" = "status" ] ; then
  342.     # Display a status report.
  343.     echo "Mounts:"
  344.     mount | sed "s/^/   /"
  345.     echo
  346.     echo "Drive power status:"
  347.     for disk in $HD; do
  348.         if [ -r $disk ]; then
  349.             hdparm -C $disk 2>/dev/null | sed "s/^/   /"
  350.         else
  351.             echo "   Cannot read $disk, permission denied - $0 needs to be run as root"
  352.         fi
  353.     done
  354.     echo
  355.     echo "(NOTE: drive settings affected by Laptop Mode cannot be retrieved.)"
  356.     echo
  357.     echo "Readahead states:"
  358.     cat /etc/mtab | while read DEV MP FST OPTS DUMP PASS ; do
  359.         # skip funny stuff
  360.         case "$FST" in 
  361.           rootfs|unionfs|tmpfs|squashfs|sysfs|usbfs|proc|devpts) continue
  362.           ;;
  363.         esac
  364.         if [ -b $DEV ] ; then
  365.             if [ -r $DEV ] ; then
  366.                 echo "   $DEV: $((`blockdev --getra $DEV` / 2)) kB"
  367.             else
  368.                 echo "   Cannot read $DEV, permission denied - $0 needs to be run as root"
  369.             fi
  370.         fi
  371.     done
  372.     echo
  373.     if [ -e /var/run/laptop-mode-tools/enabled ] ; then
  374.         echo "Laptop Mode Tools is allowed to run: /var/run/laptop-mode-tools/enabled exists."
  375.     else
  376.         echo "Laptop Mode Tools is NOT allowed to run: /var/run/laptop-mode-tools/enabled does not exist."
  377.     fi
  378.     echo
  379.     STATFILES="/proc/sys/vm/laptop_mode /proc/apm /proc/pmu/info /proc/sys/vm/bdflush /proc/sys/vm/dirty_ratio /proc/sys/fs/xfs/age_buffer /proc/sys/fs/xfs/sync_interval /proc/sys/fs/xfs/lm_age_buffer /proc/sys/fs/xfs/lm_sync_interval /proc/sys/vm/pagebuf/lm_flush_age /proc/sys/fs/xfs/xfsbufd_centisecs /proc/sys/fs/xfs/xfssyncd_centisecs /proc/sys/vm/dirty_background_ratio /proc/sys/vm/dirty_expire_centisecs /proc/sys/fs/xfs/age_buffer/centisecs /proc/sys/vm/dirty_writeback_centisecs /sys/devices/system/cpu/*/cpufreq/cpuinfo_*_freq /sys/devices/system/cpu/*/cpufreq/scaling_governor /proc/acpi/button/lid/*/state /proc/acpi/ac_adapter/*/state /proc/acpi/battery/*/state /sys/class/power_supply/*/online /sys/class/power_supply/*/state"
  380.     for THISFILE in $STATFILES ; do
  381.         if [ -e "$THISFILE" ] ; then
  382.             echo "$THISFILE:"
  383.             if [ -r "$THISFILE" ] ; then
  384.                 cat "$THISFILE" | sed "s/^/   /"
  385.             else
  386.                 echo "   Not accessible, permission denied - $0 needs to be run as root."
  387.             fi
  388.             echo
  389.         fi
  390.     done
  391.  
  392. elif [ "$1" != "readconfig" -a "$1" != "defaults" ] ; then
  393.  
  394. #############################################################################
  395.  
  396. KLEVEL="$(uname -r |
  397.              {
  398.                  IFS='.-' read a b c
  399.                  echo $a.$b
  400.              }
  401. )"
  402. KMINOR="$(uname -r |
  403.             {
  404.                 IFS='.-' read a b c d
  405.                 # Strip any stuff from the end -- only the initial digits are part of the KMINOR.
  406.                 echo $c | sed -e 's/\([[:digit:]]*\).*/\1/'
  407.             }
  408. )"
  409.  
  410. case "$KLEVEL" in
  411.     "2.4" ) ;;
  412.     "2.6" ) ;;
  413.     *)
  414.         echo "Unhandled kernel version: $KLEVEL ('uname -r' = '$(uname -r)')" >&2
  415.         exit 1
  416.         ;;
  417. esac
  418.  
  419. # Stop exporting everything -- what we do from here is private.
  420. set +a
  421.  
  422. if [ "$1" = "--version" ] ; then
  423.     echo "Laptop Mode Tools $LMTVERSION"
  424.     exit 0
  425. fi
  426. echo "Laptop Mode Tools $LMTVERSION" >> $OUTPUT
  427.  
  428. if [ ! -e /proc/sys/vm/laptop_mode ] ; then
  429.     echo "Kernel does not have support for laptop mode. Please apply the laptop mode" >&2
  430.     echo "patch or install a newer kernel." >&2
  431.     exit 1
  432. fi
  433.  
  434. if [ ! -w /proc/sys/vm/laptop_mode ] ; then
  435.     echo "You do not have enough privileges to enable laptop_mode." >&2
  436.     exit 1
  437. fi
  438.  
  439. INIT=0          # Display info in init script format?
  440. FORCE=0         # Force reapplying the current state?
  441. #while [ "$1" != "" ] ; do
  442. #    case "$1" in 
  443. #        init) INIT=1 ;;
  444. #        force) FORCE=1 ;;
  445. #        # Old options. We always do "auto" for any option now, but
  446. #        # we still have to accept the options.
  447. #        start) ;;
  448. #        stop) ;;
  449. #        auto) ;;
  450. #        *) echo "Unrecognized option $1."
  451. #           exit 1 ;;
  452. #    esac
  453. #    shift
  454. #done
  455.  
  456. if [ "$1" = "init" ] ||  [ "$2" = "init" ]; then
  457.        INIT=1
  458. fi
  459.  
  460. mkdir -p /var/run/laptop-mode-tools
  461.  
  462. # Used to display laptop mode state later on. This is the enabled/disabled 
  463. # state for laptop mode processing, it tells us nothing about whether laptop
  464. # mode is actually _active_.
  465. STATE=enabled
  466. if [ "$ENABLE_LAPTOP_MODE_ON_BATTERY" -eq 0 -a "$ENABLE_LAPTOP_MODE_ON_AC" -eq 0 -a "$ENABLE_LAPTOP_MODE_WHEN_LID_CLOSED" -eq 0 ] ; then
  467.     STATE=disabled
  468. fi
  469.  
  470. # Determine the power state.
  471.  
  472. # First try /sys/class/power_supply/*
  473. FOUND_SYS_CLASS_POWER_SUPPLY_AC=0
  474. ON_AC=0
  475. for POWER_SUPPLY in /sys/class/power_supply/* ; do
  476.     if [ -f $POWER_SUPPLY/type ] ; then
  477.         if [ "$(cat $POWER_SUPPLY/type)" = "Mains" ] ;then
  478.             $LM_VERBOSE && echo "Determining power state from $POWER_SUPPLY/online." >> $OUTPUT
  479.             FOUND_SYS_CLASS_POWER_SUPPLY_AC=1
  480.             if [ "$(cat $POWER_SUPPLY/online)" = 1 ] ; then
  481.                 ON_AC=1
  482.             fi
  483.         fi
  484.     fi
  485. done
  486.  
  487. if [ $FOUND_SYS_CLASS_POWER_SUPPLY_AC = 1 ] ; then
  488.     # Already found it!
  489.     $LM_VERBOSE && echo "Not trying other options, already found a power supply." >> $OUTPUT
  490. elif [ -d /proc/acpi/ac_adapter ] ; then
  491.     $LM_VERBOSE && echo "Determining power state from /proc/acpi/ac_adapter." >> $OUTPUT
  492.     ADAPTERS_FOUND=0
  493.     ON_AC=0
  494.     for ADAPTER in /proc/acpi/ac_adapter/* ; do
  495.         if [ -f $ADAPTER/state ] ; then
  496.             ADAPTERS_FOUND=1
  497.             STATUS=`awk '/^state: / { print $2 }' $ADAPTER/state`
  498.             if [ "$STATUS" = "on-line" ] ; then
  499.                 ON_AC=1
  500.             fi
  501.         fi
  502.     done
  503.     if [ "$ADAPTERS_FOUND" -eq 0 ] ; then
  504.         ON_AC=1
  505.     fi
  506. elif [ -f /proc/pmu/info ] ; then
  507.     $LM_VERBOSE && echo "Determining power state from /proc/pmu/info." >> $OUTPUT
  508.     if ( grep -q "^AC Power.*0$" /proc/pmu/info ) ; then
  509.         $LM_VERBOSE && echo "/proc/pmu/info indicates absence of AC power." >> $OUTPUT
  510.         ON_AC=0
  511.     else
  512.         # It is possible that there is no AC Power = 1 in the file,
  513.         # but we always assume AC power when we're not sure.
  514.         ON_AC=1
  515.         $LM_VERBOSE && echo "/proc/pmu/info indicates presence of AC power." >> $OUTPUT
  516.     fi
  517. elif [ -f /proc/apm ] ; then
  518.     $LM_VERBOSE && echo "Determining power state from /proc/apm." >> $OUTPUT
  519.     read D1 D2 D3 APM_AC_STATE D0 </proc/apm
  520.     if [ "$APM_AC_STATE" = "0x00" ] ; then
  521.         ON_AC=0
  522.     else
  523.         ON_AC=1
  524.     fi
  525. else
  526.     $LM_VERBOSE && echo "No /sys/class/power_supply, ACPI, APM or PMU power management information found -- assuming AC power is present." >> $OUTPUT
  527.     ON_AC=1
  528. fi
  529.  
  530.  
  531. # Determine whether to activate or deactivate laptop mode.
  532. ACTIVATE=0
  533.  
  534. if [ "$ON_AC" -eq 1 ] ; then
  535.     if [ "$ENABLE_LAPTOP_MODE_ON_AC" -ne 0 ] ; then
  536.         $LM_VERBOSE && echo "On AC power: Activating, because ENABLE_LAPTOP_MODE_ON_AC is set." >> $OUTPUT
  537.         ACTIVATE=1
  538.     else
  539.         $LM_VERBOSE && echo "On AC power: Deactivating, because ENABLE_LAPTOP_MODE_ON_AC is not set." >> $OUTPUT
  540.         ACTIVATE=0
  541.     fi
  542. else
  543.     if [ "$ENABLE_LAPTOP_MODE_ON_BATTERY" -ne 0 ] ; then
  544.         $LM_VERBOSE && echo "On battery power: Activating, because ENABLE_LAPTOP_MODE_ON_BATTERY is set." >> $OUTPUT
  545.         ACTIVATE=1
  546.     else
  547.         $LM_VERBOSE && echo "On battery power: Deactivating, because ENABLE_LAPTOP_MODE_ON_BATTERY is not set." >> $OUTPUT
  548.         ACTIVATE=0
  549.     fi
  550. fi
  551.  
  552. if [ "$ENABLE_LAPTOP_MODE_WHEN_LID_CLOSED" -ne 0 -a "$ACTIVATE" -eq 0 ] ; then
  553.     if [ -x "`which hal-find-by-property`" ] ; then
  554.         HAL_LID_BUTTON=$(hal-find-by-property --key "button.type" --string "lid")
  555.     fi
  556.     if [ "$HAL_LID_BUTTON" != "" ] ; then
  557.         HAL_LID_BUTTON_STATE=$(hal-get-property --udi $(hal-find-by-property --key "button.type" --string "lid") --key "button.state.value")
  558.         if [ "$HAL_LID_BUTTON_STATE" = "true" ] ; then
  559.             $LM_VERBOSE && echo "Setting action to \"start\" because the lid is closed (says HAL)." >> $OUTPUT
  560.             ACTIVATE=1            
  561.         fi
  562.     elif [ -f /proc/acpi/button/lid/*/state ] ; then
  563.         if ( grep -q "closed" /proc/acpi/button/lid/*/state ) ; then
  564.             $LM_VERBOSE && echo 'Setting action to "start" because the lid is closed (says /proc/acpi/button/lid/*/state).' >> $OUTPUT
  565.             ACTIVATE=1
  566.         fi
  567.     else
  568.         echo "Warning: ENABLE_LAPTOP_MODE_WHEN_LID_CLOSED is set, but there is no file"
  569.         echo "/proc/acpi/button/lid/.../state, and hal information is not available either!"
  570.     fi
  571. fi
  572.  
  573. # If the init script has not been run or has been run with the "stop"
  574. # argument, then we should never start laptop mode.
  575. if [ ! -f /var/run/laptop-mode-tools/enabled ] ; then
  576.     $LM_VERBOSE && echo "Laptop mode disabled because /var/run/laptop-mode-tools/enabled is missing." >> $OUTPUT
  577.     STATE=disabled
  578. fi
  579.  
  580. if [ "$ACTIVATE" -eq 1 -a -f /etc/default/laptop-mode ] ; then
  581.     . /etc/default/laptop-mode
  582.     if ! ( echo "$ENABLE_LAPTOP_MODE" |grep y ) ; then
  583.         $LM_VERBOSE && echo "Not starting laptop mode because it is disabled in /etc/default/laptop-mode." >> $OUTPUT
  584.         STATE=disabled
  585.     fi
  586. fi
  587.  
  588. if [ "$1" = "start" ]; then
  589.        STATE=enabled
  590.        ACTIVATE=1;
  591. elif [ "$1" = "stop" ] ||  [ "$2" = "stop" ]; then
  592.        STATE=disabled
  593.        ACTIVATE=0;
  594. fi
  595.  
  596. # Check whether we are allowed to activate the data-loss-sensitive stuff.
  597. # If the battery charge is too low, we want to disable this, but not the
  598. # other power-saving stuff.
  599.  
  600. if [ "$ACTIVATE" -eq 0 ] ; then
  601.     ACTIVATE_WITH_POSSIBLE_DATA_LOSS=0
  602. elif [ "$ON_AC" = 1 ] ; then
  603.     $LM_VERBOSE && echo "On AC, not checking minimum battery charge." >> $OUTPUT
  604.     ACTIVATE_WITH_POSSIBLE_DATA_LOSS=1
  605. else
  606.     ACTIVATE_WITH_POSSIBLE_DATA_LOSS=1
  607.     ENOUGH_CHARGE=0
  608.     ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=0
  609.  
  610.     # Weird way of checking that /sys/class/power_supply is not empty -- but it works.
  611.     if [ "$(echo /sys/class/power_supply/*)" != '/sys/class/power_supply/*' ] ; then
  612.         $LM_VERBOSE && echo 'Not on AC and we have battery information in /sys/class/power_supply/BAT* -- checking minimum battery charge.' >> $OUTPUT
  613.         for BATT in /sys/class/power_supply/* ; do
  614.             BATT_TYPE=$(cat $BATT/type)
  615.             $LM_VERBOSE && echo "$BATT is of type $BATT_TYPE." >> $OUTPUT
  616.             if [ "$BATT_TYPE" != "Battery" ] ; then
  617.                 $LM_VERBOSE && echo "Not of type \"Battery\", skipping." >> $OUTPUT
  618.             else
  619.                 PREV_ENOUGH_CHARGE=$ENOUGH_CHARGE
  620.                 PREV_ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=$ENOUGH_CHARGE_TO_PREVENT_HIBERNATION
  621.  
  622.                 $LM_VERBOSE && echo "Checking levels for $BATT." >> $OUTPUT
  623.                 PRESENT=$(cat $BATT/present)
  624.                 $LM_VERBOSE && echo "Present: $PRESENT." >> $OUTPUT
  625.  
  626.                 # Only do if the battery is present
  627.                 if [ "$PRESENT" -eq 1 ] ; then
  628.                     FOUND_AN_ENABLED_CHECK=0
  629.                     FOUND_AN_ENABLED_HIBERNATION_CHECK=0
  630.             
  631.                     # Get the remaining capacity.
  632.                     IN_UAH=0
  633.                     IN_UWH=0
  634.                     if [ -f $BATT/charge_now ] ; then                    
  635.                         REMAINING=$(cat $BATT/charge_now)
  636.                         IN_UAH=1 # charge_* is in microAmpere-hours
  637.                     elif [ -f $BATT/energy_now ] ; then
  638.                         REMAINING=$(cat $BATT/energy_now)
  639.                         IN_UWH=1 # energy_* is in microWatt-hours
  640.                     else
  641.                         REMAINING=0
  642.                     fi
  643.                     if [ -z "$REMAINING" -o "$REMAINING" -eq 0 ] ; then
  644.                         $LM_VERBOSE && echo "Battery does not report remaining charge. Perhaps it is not present?" >> $OUTPUT
  645.                         REMAINING=0
  646.                     fi
  647.                     $LM_VERBOSE && echo "Remaining charge: $REMAINING" >> $OUTPUT
  648.  
  649.                     if [ -f $BATT/charge_full_design ] ; then
  650.                         CAPACITY=$(cat $BATT/charge_full_design)
  651.                     elif [ -f $BATT/energy_full_design ] ; then
  652.                         CAPACITY=$(cat $BATT/energy_full_design)
  653.                     else
  654.                         CAPACITY=0
  655.                     fi
  656.                     if [ -z "$CAPACITY" -o "$CAPACITY" -eq 0 ] ; then
  657.                         $LM_VERBOSE && echo "Battery does not report design full charge, using non-design full charge." >> $OUTPUT                    
  658.                         if [ -f $BATT/charge_full ] ; then
  659.                             CAPACITY=$(cat $BATT/charge_full)
  660.                         elif [ -f $BATT/energy_full_design ] ; then
  661.                             CAPACITY=$(cat $BATT/energy_full)
  662.                         else
  663.                             CAPACITY=0
  664.                         fi
  665.                         if [ -z "$CAPACITY" -o "$CAPACITY" -eq 0] ; then
  666.                             $LM_VERBOSE && echo "Battery does not report non-design full charge." >> $OUTPUT                    
  667.                             CAPACITY=0
  668.                         fi
  669.                     fi
  670.                     $LM_VERBOSE && echo "Full capacity: $CAPACITY" >> $OUTPUT
  671.  
  672.                     # Check the charge percentage
  673.                     if [ "$MINIMUM_BATTERY_CHARGE_PERCENT" -ne 0 ] ; then
  674.                         FOUND_AN_ENABLED_CHECK=1
  675.                         if [ "$CAPACITY" -eq 0 ] ; then
  676.                             echo "WARNING: Battery does not report a capacity. Minimum battery"
  677.                             echo "charge checking does not work without a design capacity."
  678.                             ENOUGH_CHARGE=1
  679.                         elif [ "$(($REMAINING * 100 / $CAPACITY))" -ge "$MINIMUM_BATTERY_CHARGE_PERCENT" ] ; then
  680.                             ENOUGH_CHARGE=1
  681.                         fi
  682.                     fi
  683.                     if [ "$AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT" -ne 0 ] ; then
  684.                         FOUND_AN_ENABLED_HIBERNATION_CHECK=1
  685.                         if [ "$CAPACITY" -eq 0 ] ; then
  686.                             echo "WARNING: Battery does not report a design capacity. Auto hibernation"
  687.                             echo "does not work without a design capacity."
  688.                             ENOUGH_CHARGE=1
  689.                         elif [ "$(($REMAINING * 100 / $CAPACITY))" -ge "$AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT" ] ; then
  690.                             ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=1
  691.                         fi
  692.                     fi
  693.  
  694.                     ALARM_LEVEL=$(cat $BATT/alarm)
  695.                     if [ "$ALARM_LEVEL" -ne 0 ] ; then
  696.                         if [ "$REMAINING" -le "$ALARM_LEVEL" ] ; then                
  697.                             # Restore the state we had before checking this battery, so that
  698.                             # this battery does not count as having enough charge.
  699.                             ENOUGH_CHARGE=$PREV_ENOUGH_CHARGE
  700.                         elif [ "$FOUND_AN_ENABLED_CHECK" -eq 0 ] ; then
  701.                             # This is the only check that is enabled. In that case a non-critical
  702.                             # battery level counts as "enough". (If we would count non-critical
  703.                             # battery levels as enough *always*, then the other settings would
  704.                             # have no effect; this is only a final fallback.)
  705.                             ENOUGH_CHARGE=1
  706.                         fi
  707.                     fi
  708.                     if [ "$AUTO_HIBERNATION_ON_CRITICAL_BATTERY_LEVEL" -ne 0 ] ; then
  709.                         if [ "$REMAINING" -le "$ALARM_LEVEL" ] ; then                
  710.                             ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=$PREV_ENOUGH_CHARGE_TO_PREVENT_HIBERNATION
  711.                         elif [ "$FOUND_AN_ENABLED_HIBERNATION_CHECK" -eq 0 ] ; then
  712.                             ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=1
  713.                         fi
  714.                     fi
  715.                     
  716.                     #
  717.                     # Fallback: hard values
  718.                     #
  719.                     if [ "$IN_UAH" -ne 0 ] ; then
  720.                         if [ "$MINIMUM_BATTERY_CHARGE_MAH" -ne 0 ] ; then
  721.                             FOUND_AN_ENABLED_CHECK=1
  722.                             if [ "$REMAINING" -ge $((1000*"$MINIMUM_BATTERY_CHARGE_MAH")) ] ; then
  723.                                 ENOUGH_CHARGE=1
  724.                             fi
  725.                         fi
  726.                         if [ "$AUTO_HIBERNATION_BATTERY_CHARGE_MAH" -ne 0 ] ; then
  727.                             FOUND_AN_ENABLED_HIBERNATION_CHECK=1
  728.                             if [ "$REMAINING" -ge $((1000*"$AUTO_HIBERNATION_BATTERY_CHARGE_MAH")) ] ; then
  729.                                 ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=1
  730.                             fi
  731.                         fi
  732.                     elif [ "$IN_UWH" -ne 0 ] ; then
  733.                         if [ "$MINIMUM_BATTERY_CHARGE_MWH" -ne 0 ] ; then
  734.                             FOUND_AN_ENABLED_CHECK=1
  735.                             if [ "$REMAINING" -ge $((1000*"$MINIMUM_BATTERY_CHARGE_MWH")) ] ; then
  736.                                 ENOUGH_CHARGE=1
  737.                             fi
  738.                         fi
  739.                         if [ "$AUTO_HIBERNATION_BATTERY_CHARGE_MWH" -ne 0 ] ; then
  740.                             FOUND_AN_ENABLED_HIBERNATION_CHECK=1
  741.                             if [ "$REMAINING" -ge $((1000*"$AUTO_HIBERNATION_BATTERY_CHARGE_MWH")) ] ; then
  742.                                 ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=1
  743.                             fi
  744.                         fi
  745.                     else
  746.                         echo "Failed to determine battery charge. Battery charge units are not in"
  747.                         echo "mWh, uWh, mAh or uAh."
  748.                     fi
  749.                                         
  750.                 else
  751.                     $LM_VERBOSE && echo "Battery is not present." >> $OUTPUT
  752.                 fi
  753.             fi
  754.         done        
  755.     elif [ "$(echo /proc/acpi/battery/*)" != '/proc/acpi/battery/*' ] ; then
  756.         $LM_VERBOSE && echo "Not on AC and we have batteries in /proc/acpi/battery -- checking minimum battery charge." >> $OUTPUT
  757.         for BATT in /proc/acpi/battery/* ; do
  758.             PREV_ENOUGH_CHARGE=$ENOUGH_CHARGE
  759.             PREV_ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=$ENOUGH_CHARGE_TO_PREVENT_HIBERNATION
  760.  
  761.             BATT_STATE=$BATT/state
  762.             BATT_INFO=$BATT/info
  763.             $LM_VERBOSE && echo "Checking info and state for $BATT." >> $OUTPUT
  764.  
  765.             # Only do if the battery is present
  766.             if ( grep -q 'present:.*yes' $BATT_INFO ) ; then
  767.                 FOUND_AN_ENABLED_CHECK=0
  768.                 FOUND_AN_ENABLED_HIBERNATION_CHECK=0
  769.             
  770.                 # Get the remaining capacity.
  771.                 REMAINING=`grep "remaining capacity:" $BATT_STATE | sed  "s/.* \([0-9][0-9]* \).*/\1/" `
  772.                 if [ -z "$REMAINING" ] ; then
  773.                     $LM_VERBOSE && echo "Battery does not report remaining charte. Perhaps it is not present?" >> $OUTPUT
  774.                     REMAINING=0
  775.                 fi
  776.                 $LM_VERBOSE && echo "Remaining charge: $REMAINING" >> $OUTPUT
  777.  
  778.                 CAPACITY=`grep "design capacity:" $BATT_INFO | sed  "s/.* \([0-9][0-9]* \).*/\1/" `
  779.                 if [ -z "$CAPACITY" ] ; then
  780.                     $LM_VERBOSE && echo "Battery does not report capacity. Perhaps it is not present?" >> $OUTPUT
  781.                     CAPACITY=0
  782.                 fi
  783.                 $LM_VERBOSE && echo "Design capacity: $CAPACITY" >> $OUTPUT
  784.  
  785.                 # Check the charge percentage
  786.                 if [ "$MINIMUM_BATTERY_CHARGE_PERCENT" -ne 0 ] ; then
  787.                     FOUND_AN_ENABLED_CHECK=1
  788.                     if [ "$CAPACITY" -eq 0 ] ; then
  789.                         echo "WARNING: Battery does not report a design capacity. Minimum battery"
  790.                         echo "charge checking does not work without a design capacity."
  791.                         ENOUGH_CHARGE=1
  792.                     elif [ "$(($REMAINING * 100 / $CAPACITY))" -ge "$MINIMUM_BATTERY_CHARGE_PERCENT" ] ; then
  793.                         ENOUGH_CHARGE=1
  794.                     fi
  795.                 fi
  796.                 if [ "$AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT" -ne 0 ] ; then
  797.                     FOUND_AN_ENABLED_HIBERNATION_CHECK=1
  798.                     if [ "$CAPACITY" -eq 0 ] ; then
  799.                         echo "WARNING: Battery does not report a design capacity. Auto hibernation"
  800.                         echo "does not work without a design capacity."
  801.                         ENOUGH_CHARGE=1
  802.                     elif [ "$(($REMAINING * 100 / $CAPACITY))" -ge "$AUTO_HIBERNATION_BATTERY_CHARGE_PERCENT" ] ; then
  803.                         ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=1
  804.                     fi
  805.                 fi
  806.  
  807.                 #
  808.                 # Fallback: hard values.
  809.                 #
  810.                 # Determine the reporting unit.
  811.                 IN_MAH=0
  812.                 IN_MWH=0
  813.                 if ( grep -q mWh $BATT_INFO ) ; then
  814.                     IN_MWH=1
  815.                 elif ( grep -q mAh $BATT_INFO ) ; then
  816.                     IN_MAH=1
  817.                 fi
  818.             
  819.                 if [ "$IN_MAH" -ne 0 ] ; then
  820.                     if [ "$MINIMUM_BATTERY_CHARGE_MAH" -ne 0 ] ; then
  821.                         FOUND_AN_ENABLED_CHECK=1
  822.                         if [ "$REMAINING" -ge "$MINIMUM_BATTERY_CHARGE_MAH" ] ; then
  823.                             ENOUGH_CHARGE=1
  824.                         fi
  825.                     fi
  826.                     if [ "$AUTO_HIBERNATION_BATTERY_CHARGE_MAH" -ne 0 ] ; then
  827.                         FOUND_AN_ENABLED_HIBERNATION_CHECK=1
  828.                         if [ "$REMAINING" -ge "$AUTO_HIBERNATION_BATTERY_CHARGE_MAH" ] ; then
  829.                             ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=1
  830.                         fi
  831.                     fi
  832.                 elif [ "$IN_MWH" -ne 0 ] ; then
  833.                     if [ "$MINIMUM_BATTERY_CHARGE_MWH" -ne 0 ] ; then
  834.                         FOUND_AN_ENABLED_CHECK=1
  835.                         if [ "$REMAINING" -ge "$MINIMUM_BATTERY_CHARGE_MWH" ] ; then
  836.                             ENOUGH_CHARGE=1
  837.                         fi
  838.                     fi
  839.                     if [ "$AUTO_HIBERNATION_BATTERY_CHARGE_MWH" -ne 0 ] ; then
  840.                         FOUND_AN_ENABLED_HIBERNATION_CHECK=1
  841.                         if [ "$REMAINING" -ge "$AUTO_HIBERNATION_BATTERY_CHARGE_MWH" ] ; then
  842.                             ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=1
  843.                         fi
  844.                     fi
  845.                 else
  846.                     echo "Failed to determine battery charge. Battery charge units are not in"
  847.                     echo "mWh or mAh."
  848.                 fi
  849.             
  850.                 CAP_STATE=`sed -r 's/^capacity state:\s*(.*)\s*$/\1/;t;d' "$BATT_STATE"`
  851.                 if [ "$DISABLE_LAPTOP_MODE_ON_CRITICAL_BATTERY_LEVEL" -ne 0 ] ; then
  852.                     if [ "$CAP_STATE" = "critical" ] ; then                
  853.                         # Restore the state we had before checking this battery, so that
  854.                         # this battery does not count as having enough charge.
  855.                         ENOUGH_CHARGE=$PREV_ENOUGH_CHARGE
  856.                     elif [ "$FOUND_AN_ENABLED_CHECK" -eq 0 ] ; then
  857.                         # This is the only check that is enabled. In that case a non-critical
  858.                         # battery level counts as "enough". (If we would count non-critical
  859.                         # battery levels as enough *always*, then the other settings would
  860.                         # have no effect; this is only a final fallback.)
  861.                         ENOUGH_CHARGE=1
  862.                     fi
  863.                 fi
  864.                 if [ "$AUTO_HIBERNATION_ON_CRITICAL_BATTERY_LEVEL" -ne 0 ] ; then
  865.                     if [ "$CAP_STATE" = "critical" ] ; then                
  866.                         ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=$PREV_ENOUGH_CHARGE_TO_PREVENT_HIBERNATION
  867.                     elif [ "$FOUND_AN_ENABLED_HIBERNATION_CHECK" -eq 0 ] ; then
  868.                         ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=1
  869.                     fi
  870.                 fi
  871.             else
  872.                 $LM_VERBOSE && echo "Battery is not present." >> $OUTPUT
  873.             fi
  874.         done
  875.     else
  876.         ENOUGH_CHARGE=1
  877.         ENOUGH_CHARGE_TO_PREVENT_HIBERNATION=1
  878.         $LM_VERBOSE && echo "Not on AC and could not check battery state -- data loss sensitive features stay enabled and auto-hibernation will not work." >> $OUTPUT
  879.     fi
  880.     if [ "$ENABLE_AUTO_HIBERNATION" -ne 0 -a "$ENOUGH_CHARGE_TO_PREVENT_HIBERNATION" -eq 0 ] ; then
  881.         $LM_VERBOSE && echo "None of the batteries have a charge above the auto-hibernation level." >> $OUTPUT
  882.         $LM_VERBOSE && echo "Starting hibernation." >> $OUTPUT
  883.         $HIBERNATE_COMMAND
  884.         # Don't continue -- if things are configured correctly, then we
  885.         # will be called on resume.
  886.         exit 0            
  887.     fi
  888.     if [ "$ENOUGH_CHARGE" -eq 0 ] ; then
  889.         $LM_VERBOSE && echo "None of the batteries have a charge above the minimum level." >> $OUTPUT
  890.         $LM_VERBOSE && echo "Deactivating data loss sensitive features." >> $OUTPUT
  891.         ACTIVATE_WITH_POSSIBLE_DATA_LOSS=0
  892.     fi
  893. fi
  894.  
  895. if [ "$INIT" -eq 0 ] ; then
  896.     echo -n "Laptop mode "
  897. fi
  898.  
  899. WAS_ACTIVE=0
  900.  
  901. if [ "$ACTIVATE" -eq 1 ] ; then
  902.     echo -n "$STATE, active"
  903.     if [ "$ACTIVATE_WITH_POSSIBLE_DATA_LOSS" -eq 0 ] ; then
  904.         echo -n " (Data-loss sensitive features disabled.)"
  905.     fi
  906.     echo
  907. else
  908.     echo "$STATE, not active"
  909. fi
  910.  
  911.  
  912.  
  913.  
  914.  
  915. # Finally, call laptop-mode-tools modules. The modules can use the settings
  916. # from the config files, but they may NOT assume the settings actually exist,
  917. # as no defaults have been given for them.
  918.  
  919. # Note that the /usr/local/lib path is deprecated.
  920. export FORCE STATE ON_AC ACTIVATE ACTIVATE_WITH_POSSIBLE_DATA_LOSS KLEVEL KMINOR WAS_ACTIVE LM_VERBOSE
  921. for SCRIPT in /usr/share/laptop-mode-tools/modules/* /usr/local/lib/laptop-mode-tools/modules/* /usr/local/share/laptop-mode-tools/modules/* /etc/laptop-mode/modules/*; do
  922.     if [ -x "$SCRIPT" ] ; then
  923.         $LM_VERBOSE && echo "Invoking module $SCRIPT." >> $OUTPUT
  924.         "$SCRIPT" >> $OUTPUT
  925.     else
  926.         $LM_VERBOSE && echo "Module $SCRIPT is not executable." >> $OUTPUT
  927.     fi
  928. done
  929.  
  930.  
  931. exit 0
  932.  
  933. # This fi closes the if for "readconfig". If I would have indented this one
  934. # I would have indented the whole file. :)
  935. fi
  936.